class View {
  constructor() {
    this.name = '';
    this.fn1 = {};
    this.fn2 = {};
  }

  play(name, listen = true) {
    if (this.name == name) return;
    $('.page').hide();
    $(`.${name}-page`).show();
    $(`.${name}-page input`).focus();

    listen && this.fn2[this.name] && this.fn2[this.name]();
    this.name = name;
    listen && this.fn1[name] && this.fn1[name]();
  }

  loading(info, fn, name, listen = true) {
    app.loadingInfo = info || '加载中...';
    $('.loading-page').fadeIn();
    // this.play('loading');
    $(`.${this.name}-page`).hide();

    new Promise(success => fn.call(null, success))
    .then(() => {
      $('.loading-page').fadeOut();
      if(name){
        this.play(name, listen);
      } else {
        $(`.${this.name}-page`).show();
      }
    })
  }

  listen(name, fn1, fn2) {
    fn1 && (this.fn1[name] = fn1);
    fn2 && (this.fn2[name] = fn2);
  }
}